#!/bin/bash
# 设置定时任务

PROJECT_DIR="/home/abdusalam/development/football_api"
PYTHON_BIN="$PROJECT_DIR/.venv/bin/python3"
SCRIPT_PATH="$PROJECT_DIR/scripts/clean_orphaned_files.py"
LOG_DIR="$PROJECT_DIR/logs"

# 确保日志目录存在
mkdir -p "$LOG_DIR"

# 创建 cron 任务
# 每天凌晨 3 点执行清理
CRON_JOB="0 3 * * * cd $PROJECT_DIR && $PYTHON_BIN $SCRIPT_PATH --delete --clean-db >> $LOG_DIR/cleanup.log 2>&1"

# 检查是否已经存在该任务
if crontab -l 2>/dev/null | grep -q "clean_orphaned_files.py"; then
    echo "⚠️  定时任务已存在"
    echo "当前的 crontab:"
    crontab -l | grep "clean_orphaned_files.py"
else
    # 添加到 crontab
    (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
    echo "✅ 定时任务已添加"
    echo "任务详情: 每天凌晨 3 点执行清理"
fi

echo ""
echo "📋 当前所有定时任务:"
crontab -l

echo ""
echo "📝 使用说明:"
echo "  - 查看日志: tail -f $LOG_DIR/cleanup.log"
echo "  - 手动运行(预览): cd $PROJECT_DIR && $PYTHON_BIN $SCRIPT_PATH"
echo "  - 手动运行(删除): cd $PROJECT_DIR && $PYTHON_BIN $SCRIPT_PATH --delete --clean-db"
echo "  - 移除定时任务: crontab -e (然后删除相关行)"

